//
//  HMStatusToolBar.swift
//  WeiBo14
//
//  Created by HM on 16/3/6.
//  Copyright © 2016年 HM. All rights reserved.
//  转发评论赞的视图,里面管理三个按钮

import UIKit
/**
 以转发来说,如果有转发数据,那么就显示对应数量
 - 如果低于10000 --> 直接显示数字
 - 如果低于11000 --> 显示1万
 - 如果高于11000(12003) --> 显示 1.x 万
*/

class HMStatusToolBar: UIView {
    
    private var retweetButton: UIButton!
    private var commentButton: UIButton!
    private var unlikeButton: UIButton!
    
    // 用于设置三个按钮显示数据的视图模型
    var statusViewModel: HMStatusViewModel? {
        didSet{
            // 给转发按钮设置数据
            // 拿到转发按钮
            retweetButton.setTitle(statusViewModel?.repostsCountStr, forState: UIControlState.Normal)
            commentButton.setTitle(statusViewModel?.commentsCountStr, forState: UIControlState.Normal)
            unlikeButton.setTitle(statusViewModel?.attitudesCountStr, forState: UIControlState.Normal)
        }
    }

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        
        // 添加三个按钮
        retweetButton = addChildButton("timeline_icon_retweet", title: "转发")
        commentButton = addChildButton("timeline_icon_comment", title: "评论")
        unlikeButton = addChildButton("timeline_icon_unlike", title: "赞")
        
        // 添加2个分割线
        let sp1 = addSpliteView()
        let sp2 = addSpliteView()
        // 并且给三个按钮添加约束
        
        
        retweetButton.snp_makeConstraints { (make) -> Void in
            make.leading.top.bottom.equalTo(self)
            make.width.equalTo(commentButton)
        }
        commentButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(retweetButton.snp_trailing)
            make.top.bottom.equalTo(retweetButton)
            make.width.equalTo(unlikeButton)
        }
        unlikeButton.snp_makeConstraints { (make) -> Void in
            make.leading.equalTo(commentButton.snp_trailing)
            make.top.bottom.equalTo(commentButton)
            make.trailing.equalTo(self)
        }
        
        sp1.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(retweetButton.snp_trailing)
            // 约束垂直居中
            make.centerY.equalTo(self)
        }
        
        sp2.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(commentButton.snp_trailing)
            make.centerY.equalTo(self)
        }
    }
    
    /// 添加分割线
    private func addSpliteView() -> UIView {
        let imageView = UIImageView(image: UIImage(named: "timeline_card_bottom_line"))
        addSubview(imageView)
        return imageView
    }
    /// 添加按钮
    ///
    /// - parameter imageNamed: 按钮显示的图片名
    /// - parameter title:      显示的标题
    ///
    /// - returns: <#return value description#>
    private func addChildButton(imageNamed: String, title: String) -> UIButton {
        // 初始化按钮
        let button = UIButton()
        // 设置title和图标
        button.setTitle(title, forState: UIControlState.Normal)
        button.setImage(UIImage(named: imageNamed), forState: UIControlState.Normal)
        // 设置文字大小和颜色
        button.titleLabel?.font = UIFont.systemFontOfSize(14)
        button.setTitleColor(UIColor.grayColor(), forState: UIControlState.Normal)
        
        // 设置不同状态下的背景图
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background_highlighted"), forState: UIControlState.Highlighted)
        button.setBackgroundImage(UIImage(named: "timeline_card_bottom_background"), forState: UIControlState.Normal)
        
        addSubview(button)
        
        return button
    }
}
